<?php 
/**
* 
*/
class CustomerController extends Controller
{
	
	public function index()
	{
		if (IS_POST) {
            $data['code'] = Q('post.code');
            $data['name'] = Q('post.name');
            $data['phone'] = Q('post.phone');
            if (empty($data['code'])) {
                $this->error('验证码不能为空');
            } else if (strtoupper($data['code']) !== session('code')) {
               $this->error('验证码输入错误');
            }
            if(empty($data['name']) || empty($data['phone']) ){
                $this->error('客户姓名或者手机号码不能为空');
            }
            $where=array('name' => $data['name'] ,'phone' =>$data['phone'] );
            $user = M('customer')->where($where)->find();
            if($user){
                $content =C('SMS_CONTENT');
                $status =9;
                $status=$this->sms($user['phone'],$content);
                if($status == 0){
                    $this->success('验证成功短信已经发送到'.$user['phone']);
                }else{
                    $this->error('系统错误短信发送失败');
                }
            }else{
            	$this->error('客户姓名或者手机号码不正确');
            }
        } else {
            $this->display();
        }
	}
	public function code()
    {
        C(array(
                "CODE_BG_COLOR" => "#ffffff", "CODE_LEN" => 4,
                "CODE_FONT_SIZE" => 20, "CODE_WIDTH" => 120,
                "CODE_HEIGHT" => 35,
            )
        );
        $code = new Code();
        $code->show();
        exit;
    }

    private function sms($phone,$content){
        $username = "qizun";
        $password = "367BRSEH";
        $sendto =$phone;
        $message =urlencode($content) ;//内容解码
        $url="http://124.173.70.59:8081/SmsAndMms/mt?";
        $curlPost = 'Sn='.$username.'&Pwd='.$password.'&mobile='.$sendto.'&content='.$message.'';

        $ch = curl_init();//初始化curl
        curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  //允许curl提交后,网页重定向  
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        preg_match("/\d+/",$data,$regs);
        return $regs[0];
    }

}
 ?>